class compiler {
    constructor (vm) {
        this.el = vm.$el;
        this.vm = vm;
        this.compile(this.el);
    }
    // 编译模板、处理文本节点和元素节点
    compile (el) {
        let childNodes = el.childNodes;
        Array.from(childNodes).forEach(node => {
            if (this.isTextNode(node)) {
                this.compileText(node);
            } else if (this.isElementNode(node)) {
                this.compileElement(node);
            }
            // 子节点还有子节点
            if (node.childNodes && node.childNodes.length) {
                this.compile(node);
            }
        });
    }
    // 编译元素节点-处理指令
    compileElement (node) {
        // v-text="msg" v-model="msg"
        // 遍历所有属性节点
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name; // v-text v-model
            // 判断是不是指令v-
            if (this.isDirective(attrName)) {
                attrName = attrName.substr(2); // text model
                let key = attr.value; // msg
                // 判断是否on:click
                if (this.isEvent(attrName)) {
                    attrName = attrName.substr(3);
                    this.doEvent(node, key, attrName);
                    return;
                }
                this.update(node, key, attrName);
            }
        });
    }
    update (node, key, attrName) {
        let updateFn = this[attrName + 'Updater'];
        updateFn && updateFn.call(this, node, this.vm[key], key);
    }
    doEvent (node, key, attrName) {
        let eventFn = this.vm.$options && this.vm.$options.methods && this.vm.$options.methods[key];
        node.addEventListener(attrName, eventFn.bind(this.vm));
    }
    // 处理v-text指令
    textUpdater (node, value, key) {
        node.textContent = value;
        new Watcher(this.vm, key, (newValue)=>{
            node.textContent = newValue;
        });
    }
    // 处理v-model指令
    modelUpdater (node, value, key) {
        node.value = value;
        new Watcher(this.vm, key, (newValue)=>{
            node.value = newValue;
        });
        // 双向绑定
        node.addEventListener('input', ()=>{
            this.vm[key] = node.value;
        });
    }
    // 处理v-html指令
    htmlUpdater (node, value, key) {
        node.innerHTML = value;
        new Watcher(this.vm, key, (newValue)=>{
            node.innerHTML = newValue;
        });
    }
    // 处理v-on:click

    // 编译文本节点-处理插值表达式
    compileText (node) {
        // {{msg}}
        let reg = /\{\{(.+?)\}\}/;
        let value = node.textContent;
        if (reg.test(value)) {
            let key = RegExp.$1.trim();
            node.textContent = value.replace(reg, this.vm[key]);

            // 创建watcher对象，当数据改变时更新视图
            new Watcher(this.vm, key, (newValue)=>{
                node.textContent = newValue;
            });
        }
    }
    // 判断元素属性是否是指令
    isDirective (attrName) {
        return attrName.startsWith('v-');
    }
    // 判断是否元素节点
    isElementNode (node) {
        return node.nodeType === 1;
    }
    // 判断是否文本节点
    isTextNode (node) {
        return node.nodeType === 3;
    }
    // 判断是否事件 v-on:click  @click
    isEvent (attrName) {
        return attrName.startsWith('on');
    }
}